<?php
namespace catchAdmin\propaganda\controller;

use catcher\CatchResponse;
use app\common\utils\Upfile;
use catcher\base\CatchRequest;
use catcher\base\CatchController;
use app\common\model\Project as ProjectModel;
use app\common\model\PropagandaEducationSatisfaction as PESModel;
use catchAdmin\propaganda\request\SaveSatisfiedRequest;
use app\common\model\PropagandaEducation as PropagandaEducationModel;
use catcher\exceptions\FailedException;

class Education extends CatchController
{
  protected $propagandaEducation;

  public function __construct(PropagandaEducationModel $propagandaEducation)
  {
      $this->propagandaEducation = $propagandaEducation;
  }

  /**
   * 列表
   *
   * @time 2020年01月09日
   * @param CatchRequest $request
   * @return \think\response\Json
   * @throws \think\db\exception\DbException
   */
  public function index(): \think\response\Json
  {
      return CatchResponse::paginate($this->propagandaEducation->getList());
  }

  /**
   * 保存
   *
   * @time 2020年01月09日
   * @param CatchRequest $request
   * @return \think\response\Json
   */
  public function save(CatchRequest $request): \think\response\Json
  {
      $param = $request->param();
      $param['created_ymd'] = date("Y-m-d");
      return CatchResponse::success($this->propagandaEducation->addData($param));
  }

  /**
   * 更新
   *
   * @time 2020年01月09日
   * @param $id
   * @param CatchRequest $request
   * @return \think\response\Json
   */
  public function update($id, CatchRequest $request): \think\response\Json
  {
      return CatchResponse::success($this->propagandaEducation->saveData($id, $request->param()));
  }

  /**
   * 删除
   *
   * @time 2020年01月09日
   * @param $id
   * @return \think\response\Json
   */
  public function delete($id): \think\response\Json
  {
      return CatchResponse::success($this->propagandaEducation->destroy($id));
  }

  /**
   * 详情
   *
   * @time 2020年01月09日
   * @param $id
   * @return \think\response\Json
   */
  public function detailed($id): \think\response\Json
  {
      $ret = $this->propagandaEducation->getDetail($id);

      return CatchResponse::success($ret);
  }

   /**
    * 上传文件
    *
    * @param CatchRequest $request
    * @return \think\response\Json
    */    
    public function uploadfile(CatchRequest $request): \think\response\Json
    {
        return CatchResponse::success((new Upfile)->upLoad($request->file(), $request->param('type')));
    }    

    /**
     * 获取宣教类别树型
     *
     * @param CatchRequest $request
     * @return \think\response\Json
     */ 
    public function get_education()
    {
        return CatchResponse::success($this->propagandaEducation->getDatas());
    }

    /**
     * 获取满意度问卷
     *
     * @return \think\response\Json
     */
    public function get_satisfied_quest(): \think\response\Json
    {
        $ret = (new ProjectModel())->where('survey_type', 2)->field('projectId,name')->select();
        return CatchResponse::success($ret);
    }

    /**
     * 宣教满意度列表
     *
     * @param CatchRequest $request
     * @return \think\response\Json
     */
    public function ed_satisfied_quest(CatchRequest $request): \think\response\Json
    {
        $req = $request->param();

        if (!isset($req['classify']) || empty($req['classify'])) {
            return CatchResponse::success();
        }
        return CatchResponse::success((new PESModel())->getDatas(['classify' => $req['classify']]));
    }

    /**
     * 保存宣教满意度调查问卷
     *
     * @param SaveSatisfiedRequest $request
     * @return \think\response\Json
     */
    public function save_satisfied_quest(SaveSatisfiedRequest $request): \think\response\Json
    {
        $req = $request->param();
        $req['creator_name'] = request()->user()->username;
        $ret = (new PESModel())->where('classify', $req['classify'])->field('id')->find();

        if (!empty($ret['id'])) {
            return CatchResponse::fail('该分类下已有满意度问卷，不可再次创建');
        }
        return CatchResponse::success((new PESModel())->addData($req));
    }

    /**
     * 更新宣教满意度调查问卷(待定)
     *
     * @param CatchRequest $request
     * @return \think\response\Json
     */
    public function up_satisfied_quest(CatchRequest $request): \think\response\Json
    {
        $req = $request->param();
        $req['creator_name'] = request()->user()->username;
        if (!isset($req['id']) || empty($req['id'])) {
            throw new FailedException('缺少id参数');
        }
        return CatchResponse::success((new PESModel())->saveData($req['id'], $req));
    }

    /**
     * 删除宣教满意度调查问卷
     *
     * @param CatchRequest $request
     * @return \think\response\Json
     */
    public function del_satisfied_quest(CatchRequest $request): \think\response\Json
    {
        $req = $request->param();

        if (!isset($req['id']) || empty($req['id'])) {
            throw new FailedException('缺少id参数');
        }
        return CatchResponse::success((new PESModel())->delData($req['id']));
    }
}